package com.wanpinghui.supplier.user.order;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.LinearLayout;

import com.google.gson.Gson;
import com.malinskiy.superrecyclerview.OnMoreListener;
import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.user.order.adapter.OrderAdapter;
import com.wanpinghui.supplier.utils.ActivityJumpUtil;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.order.OrderService;
import com.wanpinghui.wphlibrary.model.order.bean.OrderItemBean;
import com.wanpinghui.wphlibrary.model.order.bean.PageDataBean;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import retrofit.callback.YRequestCallback;

import static com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter.TYPE_PULL_MORE;

/**
 * 功能说明：
 * Created by caitiangui
 * 创建时间： 2017/12/27.
 */
@ContentView(R.layout.layout_activity_history_order)
public class HistoryOrderActivity extends BaseActivity {

    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser tpl_title;

    @ViewInject(R.id.super_recycle_view)
    private SuperRecyclerView super_recycle_view;

    @ViewInject(R.id.ll_empty_view)
    private LinearLayout ll_empty_view;


    private List<OrderItemBean> orderItemBeanList;

    private OrderAdapter adapter;

    private int currpage = 1;
    private int userId;


    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
        tpl_title.setTitle("历史订单");
        adapter = new OrderAdapter(this);
        super_recycle_view.setLayoutManager(new LinearLayoutManager(this));
        super_recycle_view.setAdapter(adapter);
    }

    @Override
    public void bindEvents() {
        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter.setListener(new OrderAdapter.setOnItemListener() {
            @Override
            public void onClick(OrderItemBean bean) {
                Intent intent = new Intent(HistoryOrderActivity.this, OrderDetailActivity.class);
                intent.putExtra("order", new Gson().toJson(bean));
                startActivity(intent);
            }
        });
        findViewById(R.id.tv_see_my).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityJumpUtil.startProductList(HistoryOrderActivity.this);
            }
        });
        super_recycle_view.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currpage = 1;
                searchPersonalList(currpage, BaseRecyclerViewAdapter.TYPE_PULL_REFRESH);
            }
        });

    }

    @Override
    public void initData() {
        if (getIntent() != null) {
            userId = getIntent().getIntExtra("id", 0);
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        currpage = 1;
        searchPersonalList(currpage, BaseRecyclerViewAdapter.TYPE_PULL_REFRESH);
    }

    private void searchPersonalList(int page, final int type) {
        WPHSdk.getService(OrderService.class).searchPersonalList(page, userId, new YRequestCallback<PageDataBean<OrderItemBean>>() {
            @Override
            public void onSuccess(final PageDataBean var1) {
                if (type == BaseRecyclerViewAdapter.TYPE_PULL_REFRESH) {
                    orderItemBeanList = var1.getPdList();
                    if (orderItemBeanList != null && orderItemBeanList.size() > 0) {
                        ll_empty_view.setVisibility(View.GONE);
                        super_recycle_view.setVisibility(View.VISIBLE);
                    } else {
                        ll_empty_view.setVisibility(View.VISIBLE);
                        super_recycle_view.setVisibility(View.GONE);

                        return;
                    }
                } else {
                    orderItemBeanList.addAll(var1.getPdList());
                }
                adapter.setDatas(orderItemBeanList);
                if (adapter.getDatas().size() < var1.getTotalResult()) {
                    super_recycle_view.setupMoreListener(new OnMoreListener() {
                        @Override
                        public void onMoreAsked(int overallItemsCount, int itemsBeforeMore, int maxLastVisiblePosition) {
                            currpage = currpage + 1;
                            searchPersonalList(currpage, TYPE_PULL_MORE);
                        }
                    }, 1);
                } else {
                    super_recycle_view.hideMoreProgress();
                    super_recycle_view.removeMoreListener();
                }

            }

            @Override
            public void onFailed(int var1, String message) {
                showWarningText(message);
            }

            @Override
            public void onException(Throwable var1) {
                var1.printStackTrace();

            }
        });
    }
}
